package com.example.smart.ui.activity.home

import com.example.lib.extend.gridManager
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.adapter.home.ReservationAdapter
import com.example.smart.base.ui.activity.BaseTitleBarActivity
import com.example.smart.databinding.ActivityReservationBinding
import com.example.smart.dialog.ReservationSuccessDialog
import com.example.smart.room.bean.ReservationBean
import com.example.smart.room.factory.RoomFactory
import com.necer.painter.InnerPainter

class ReservationActivity : BaseTitleBarActivity<ActivityReservationBinding>() {
    private var nowNowChooseDate = ""
    private val mReservationRepository = RoomFactory.createReservationRepository()

    private val mDialog by lazy {
        ReservationSuccessDialog(this@ReservationActivity)
    }

    private val mAdapter by lazy {
        ReservationAdapter(this@ReservationActivity)
    }

    override fun layoutID() = R.layout.activity_reservation

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@ReservationActivity,
            statusBarColor = com.example.lib.R.color.white)
        title = getString(R.string.title_reservation)

        binding.Recycler.gridManager(3)
        binding.Recycler.adapter = mAdapter

        mReservationRepository.getAllData {
            val painter = binding.CalenderView.calendarPainter as InnerPainter
            painter.addPointList(it.map { map -> map.date })
        }
    }

    override fun initListener() {
        binding.Confirm.setOnDoubleClickListener {
            if (mAdapter.nowChoose == -1) {
                ToastUtils.showToast(getString(R.string.please_select_time))
            } else {
                mReservationRepository.insert(ReservationBean(nowNowChooseDate,
                    mAdapter.getItemAtPosition(mAdapter.nowChoose)))
                val painter = binding.CalenderView.calendarPainter as InnerPainter
                painter.addPointList(mutableListOf(nowNowChooseDate))
                mDialog.show()
            }
        }

        binding.CalenderView.setOnCalendarChangedListener { _, _, _, p3, _ ->
            nowNowChooseDate = p3.toString()
        }
    }
}